﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo01
{
    /*添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
    1、学生类：学号、姓名、年龄
    2、请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息。
    3、重复的学号不能添加。
    4、查询学生信息功能中有：1、查询所有（按学号排序）2、查询所有（按姓名排序），
    2、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出*/



    class Program
    {
        static void Main(string[] args)
        {
            List<Student> list = new List<Student>();
            while (true)
            {
                Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息。");
                int choice;
                while (true)
                {
                    try
                    {
                        choice = int.Parse(Console.ReadLine());
                        if (choice < 1 || choice > 3)
                        {
                            Console.WriteLine("请重新输入");
                            continue;
                        }
                        break;

                    }
                    catch (Exception)
                    {
                        Console.WriteLine("请重新输入");
                    }
                }

                switch (choice)
                {
                    case 1:
                        Console.WriteLine("请输入学生姓名：");
                        string name = Console.ReadLine();
                        int stuNo;
                        int stuAge;
                    
                    a: while (true)
                        {
                            try
                            {
                                Console.WriteLine("请输入学号：");
                                stuNo = int.Parse(Console.ReadLine());
                                Console.WriteLine("请输入年龄：");
                                stuAge = int.Parse(Console.ReadLine());
                                if (stuAge < 0 || stuAge > 150)
                                {
                                    Console.WriteLine("请重新输入");
                                    continue;
                                }
                                foreach (Student stu in list)
                                {
                                    if (stu.StuNo == stuNo)
                                    {
                                        Console.WriteLine("已存在此学号，请重新输入");
                                        goto a;
                                    }
                                }
                                break;
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("请重新输入");
                            }
                        }
                        Student s = new Student(name,stuNo,stuAge);
                        list.Add(s);
                        break;
                    case 2:
                        Console.WriteLine("1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
                        int choice2;
                        while (true)
                        {
                            try
                            {
                                choice2 = int.Parse(Console.ReadLine());
                                if (choice2 < 1 || choice2 > 5)
                                {
                                    Console.WriteLine("请重新输入");
                                    continue;
                                }
                                break;

                            }
                            catch (Exception)
                            {
                                Console.WriteLine("请重新输入");
                            }
                        }
                        switch (choice2)
                        {
                            case 1:
                                list.Sort(new StuNoCompare());
                                foreach (Student stu in list)
                                {
                                    Console.WriteLine(stu);
                                }
                                break;
                            case 2:
                                list.Sort(new NameCompare());
                                foreach (Student stu in list)
                                {
                                    Console.WriteLine(stu);
                                }
                                break;
                            case 3:
                                list.Sort(new AgeCompare());
                                foreach (Student stu in list)
                                {
                                    Console.WriteLine(stu);
                                }
                                break;
                            case 4:
                                Console.WriteLine("请输入学生学号");
                                int inputStuNo2;
                                bool flag2 = true;
                                while (true)
                                {
                                    try
                                    {
                                        inputStuNo2 = int.Parse(Console.ReadLine());
                                        break;
                                    }
                                    catch (Exception)
                                    {
                                        Console.WriteLine("请重新输入");
                                    }
                                }
                                for (int i = 0;i < list.Count; i++)
                                {
                                    if (list[i].StuNo == inputStuNo2)
                                    {
                                        flag2 = true;
                                        Console.WriteLine(list[i]);
                                        break;
                                    }
                                    else
                                    {
                                        flag2 = false;
                                    }
                                }
                                if (!flag2)
                                {
                                    Console.WriteLine("没有此学生");
                                }
                                break;
                            case 5:
                                Process.GetCurrentProcess().Kill();
                                break;
                            default:
                                break;
                        }
                        break;
                    case 3:
                        Console.WriteLine("请输入需要删除的学生学号");
                        int inputStuNo;
                        bool flag3 = true;
                        while (true)
                        {
                            try
                            {
                                inputStuNo = int.Parse(Console.ReadLine());
                                break;
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("请重新输入");
                            }
                        }
                        foreach (Student stu in list)
                        {
                            if (stu.StuNo == inputStuNo)
                            {
                                flag3 = true;
                                list.Remove(stu);
                                Console.WriteLine("删除成功");
                                break;
                            }
                            else
                            {
                                flag3 = false;
                            }
                        }
                        if (!flag3)
                        {
                            Console.WriteLine("没有此学生");
                        }
                        break;
                    default:
                        break;
                }
            }

        }
    }
    class Student
    {
        public string Name { get; set; }
        public int StuNo { get; set; }
        public int Age { get; set; }

        public Student(string name, int stuNo, int age)
        {
            Name = name;
            StuNo = stuNo;
            Age = age;
        }

        public Student()
        {

        }

        public override string ToString()
        {
            return $"Name:{this.Name},StuNo{this.StuNo},Age{this.Age}";
        }
    }

    class NameCompare : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

    class StuNoCompare : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.StuNo.CompareTo(y.StuNo);
        }
    }
    class AgeCompare : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
}
